package cn.zhang.reimbursement_1.service.impl;

import cn.zhang.reimbursement_1.common.CommonMethod;
import cn.zhang.reimbursement_1.config.LoginInterceptor;
import cn.zhang.reimbursement_1.mapper.RecordMapper;
import cn.zhang.reimbursement_1.model.Record;
import cn.zhang.reimbursement_1.request.LoginRequest;
import cn.zhang.reimbursement_1.tools.JsonData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhang.reimbursement_1.model.ClassOn;
import cn.zhang.reimbursement_1.service.ClassOnService;
import cn.zhang.reimbursement_1.mapper.ClassOnMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author zfs90
* @description 针对表【t_class_on】的数据库操作Service实现
* @createDate 2022-03-25 15:59:19
*/
@Service
public class ClassOnServiceImpl implements ClassOnService{

    @Autowired
    private ClassOnMapper classOnMapper;

    @Autowired
    private RecordMapper recordMapper;


    @Override
    public JsonData saveclassOn(ClassOn classOn) {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
        if (CommonMethod.checkRole(loginRequest)){
            classOnMapper.insert(classOn);
            return JsonData.buildSuccess();
        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");

    }

    @Override
    public JsonData getclassOn(String like) {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
        List<ClassOn> classOns = classOnMapper.selectList(new QueryWrapper<ClassOn>()
                .lambda().like(ClassOn::getClassOn,like));
        if (CommonMethod.checkRole(loginRequest)){
            return JsonData.buildSuccess(classOns);
        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");

    }

    @Override
    public JsonData updateclassOn(ClassOn classOn) {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
        Record record = new Record();

        if (CommonMethod.checkRole(loginRequest)){
            classOnMapper.updateById(classOn);

            record.setRealname(loginRequest.getUsername());
            record.setOperation("修改");
            record.setInsertTime(new Date());
            record.setDetails("修改了"+classOn.getClassOn());
            recordMapper.insert(record);
            return JsonData.buildSuccess();
        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");

    }

    @Override
    public JsonData delclassOn(Integer delId) {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
        ClassOn classOn = classOnMapper.selectById(delId);
        Record record = new Record();

        if (CommonMethod.checkRole(loginRequest)){
            classOnMapper.deleteById(delId);

            record.setRealname(loginRequest.getUsername());
            record.setOperation("删除");
            record.setInsertTime(new Date());
            record.setDetails("删除了"+classOn.getClassOn());
            recordMapper.insert(record);
            return JsonData.buildSuccess();
        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");

    }
}




